package com.bicon.botu.mall.component.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.component.entity.HistorySearch;
import com.bicon.botu.mall.component.entity.HotSearch;
import com.bicon.botu.mall.component.service.IHotSearchService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 热搜 前端控制器
 *
 * @author 张露晨
 * @date 2017/8/7 17:07.
 */
@RestController
@RequestMapping("/V2.0.0/hotSearch")
public class HotSearchControllerV2 extends BaseController {


    @Autowired
    private IHotSearchService iHotSearchService;

    /**
     * 热词最大数量
     */
    private static final int MEDICAL_HOT_SEARCH_NUMBER = 15;

    /**
     * 新增热词
     *
     * @param type     模块类型：商城-mall,医疗-medical（其他待定）
     * @param hotWord  热词
     * @param showFlag 显示开关：0显示，1不显示
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam("type") String type,
                             @RequestParam("hotWord") String hotWord,
                             @RequestParam("showFlag") String showFlag) {
        //参数检验
        if (StringUtils.isAnyBlank(type, hotWord, showFlag)) {
            return sendResult400();
        }
        //查询数据库是否已存在此热词
        HotSearch hotSearchMedical = iHotSearchService.selectOne(new EntityWrapper<HotSearch>()
                .eq("hot_word", hotWord).eq("type", type).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (hotSearchMedical != null) {
            return sendResult400("该热词已经添加");
        }
        //查询当前热词个数
        Integer count = new HotSearch().selectCount(new EntityWrapper<HotSearch>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL)
                .eq("type", type));
        if (MEDICAL_HOT_SEARCH_NUMBER <= count) {
            return sendResult400("最多添加15个");
        }
        return iHotSearchService.insert(type, hotWord, showFlag, count);
    }

    /**
     * 删除热词
     *
     * @param type   模块类型：商城-mall,医疗-medical（其他待定）
     * @param idJson 热词Id集合
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult delete(@RequestParam("type") String type,
                             @RequestParam("idJson") String idJson) {
        return iHotSearchService.delete(type, idJson);
    }

    /**
     * 修改热词
     *
     * @param id       热词id
     * @param hotWord  热词
     * @param showFlag 显示开关：0显示，1不显示
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam("id") String id,
                             @RequestParam("hotWord") String hotWord,
                             @RequestParam(value = "showFlag", required = false) String showFlag) {
        //判断id是否存在
        HotSearch hotSearch = new HotSearch().selectOne(new EntityWrapper<HotSearch>()
                .eq("id", id).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (hotSearch == null) {
            return sendResult400();
        }
        //查询数据库是否已存在此热词
        HotSearch hotSearch1 = new HotSearch().selectOne(new EntityWrapper<HistorySearch>()
                .eq("hot_word", hotWord).eq("type", hotSearch.getType()).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (hotSearch1 != null) {
            return new BaseResult(Constants.RESPONSE_CODE_400, "该热词已经存在！");
        }
        return iHotSearchService.update(id, hotWord, showFlag);
    }

    /**
     * 获取全部热搜
     *
     * @param type 模块类型: 商城-mall,医疗-medical（其他待定）
     * @return
     */
    @RequestMapping(value = "/getHotSearch", method = RequestMethod.POST)
    public BaseResult getHotSearchDetailFromRedis(@RequestParam("type") String type) {
        List<HotSearch> hotSearchList = new HotSearch().selectList(new EntityWrapper<HotSearch>()
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL)
                .eq("type", type).orderBy("sort", false));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, hotSearchList);
    }

}
